package com.stadium.service.impl;

import com.stadium.common.utils.SqlMapper;
import com.stadium.common.utils.UserUtils;
import com.stadium.dto.BasicsRuleDto;
import com.stadium.dto.GameEventDto;
import com.stadium.dto.RRuleDetailDto;
import com.stadium.entity.BasicsRule;
import com.stadium.mapper.BasicsRuleMP;
import com.stadium.mapper.GameEventMP;
import com.stadium.mapper.RRuleDetailMP;
import com.stadium.service.BasicsRuleSV;
import com.stadium.vo.BasicsRuleVo;
import com.stadium.vo.searchVo.BasicsRuleSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class BasicsRuleSVI implements BasicsRuleSV {
    @Autowired
    private BasicsRuleMP mapper;
    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    private RRuleDetailMP rRuleDetailMP;
    @Autowired
    private GameEventMP gameEventMP;

    @Override
    public List<BasicsRule> queryBySQL(String sql, Map<String, Object> map) {
        return sqlMapper.selectList(sql, map, BasicsRule.class);
    }

    @Override
    public void save(BasicsRuleVo basicsRuleVo) {
        BasicsRule basicsRule = new BasicsRule();
        BeanUtils.copyProperties(basicsRuleVo, basicsRule);
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        basicsRule.setUpdateUser(UserUtils.getUserId());
        basicsRule.setId(id);
        basicsRule.setCreateUser(UserUtils.getUserId());
        mapper.save(basicsRule);
    }

    @Override
    public void update(BasicsRuleVo basicsRuleVo) {
        BasicsRule basicsRule = new BasicsRule();
        BeanUtils.copyProperties(basicsRuleVo, basicsRule);
        basicsRule.setUpdateUser(UserUtils.getUserId());
        mapper.update(basicsRule);
    }

    @Override
    public void saveBatch(List<BasicsRule> list) {
        mapper.saveBatch(list);
    }

    @Override
    public void delete(String id) {
        mapper.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        mapper.deleteBatch(ids);
    }


    @Override
    public List<BasicsRuleDto> query(BasicsRuleSearchVo searchVo) {
        return mapper.query(searchVo);
    }

    @Override
    public BasicsRuleDto get(String id) {
        BasicsRuleDto basicsRuleDto = mapper.get(id);
        if (basicsRuleDto != null) {
            List<RRuleDetailDto> rRuleDetailDtos = rRuleDetailMP.getByRuleId(basicsRuleDto.getId());
            basicsRuleDto.setRRuleDetailDtoList(rRuleDetailDtos);
        }
        return basicsRuleDto;
    }

    @Override
    public int getCount(BasicsRuleSearchVo searchVo) {
        return mapper.getCount(searchVo);
    }

    @Override
    public BasicsRuleDto getByLeagueId(String leagueId) {
        GameEventDto gameEventDto = gameEventMP.get(leagueId);
        BasicsRuleDto basicsRuleDto = mapper.get(gameEventDto.getRuleId());
        if (basicsRuleDto != null) {
            List<RRuleDetailDto> rRuleDetailDtos = rRuleDetailMP.getByRuleId(basicsRuleDto.getId());
            basicsRuleDto.setRRuleDetailDtoList(rRuleDetailDtos);
        }
        return basicsRuleDto;
    }
}
